<?php
namespace Admin\Frontend\Models;

use Helpers\HttpClient;

class Admire{
    private $host;

    //文章入选等级(1:未选；2:初选；3:精选；4:佳作)
    public static $articleLevel = [
        '1' => '未选',
        '2' => '初选',
        '3' => '精选',
        '4' => '佳作'
    ];

    //文章类型(1：随感赏析；2：心情随笔；；3：命题作文)
    public static $articleType = [
        '1' => '随感赏析',
        '2' => '心情随笔',
        '3' => '命题作文'
    ];

    public static $scope = [
        '1' => '班级',
        '2' => '学校',
        '3' => '全平台'
    ];

    //是否可撤回(1:是；2:否)
    public static $recallStatus = [
        '1' => '是',
        '2' => '否'
    ];

    //支付积分状态(1:已支付 2:未支付)
    public static $payIntegralStatus = [
        '1' => '已支付',
        '2' => '未支付'
    ];

    public static $praiseStatus = [
        '1' => '已点赞',
        '2' => '未点赞'
    ];

    //删除状态(1:正常；2:删除；3:屏蔽)
    // public static $status = [
    //   '1' => '正常',
    //   '2' => '删除',
    //   '3' => '屏蔽'
    // ];

    //删除状态HTML(1:正常；2:删除；3:屏蔽)
    public static $status = [
        '1' => '<i class="fa fa-check-circle-o" style="color:green;font-size:16px;"></i>',
        '2' => '<i class="fa fa-trash-o" style="color:red;font-size:16px;"></i>',
        '3' => '<i class="fa fa-minus-circle" style="color:orange;font-size:16px;"></i>'
    ];

    //发布状态(1:发表；2:未发表)
    public static $releaseStatus = [
        '1' => '发表',
        '2' => '未发表'
    ];

    //收藏状态(1:收藏 2:未收藏)
    public static $collectStatus = [
        '1' => '收藏',
        '2' => '未收藏'
    ];

    //文章操作类型
    public static $typeCtrl = [
        'deny' => '1',
        'recover' => '2',
        'del' => '3'
    ];

    //评论人类型
    public static $userType = [
        '1' => '学生',
        '4' => '老师'
    ];

    /**
     * 设置接口host
     *
     * @param $url
     *
     * create by hxc 20171205
     */
    public function setHost($host){
        $this->host = $host;
    }

    /**
     * 默认文章列表/检索文章列表
     *
     * @param null $parameters
     * @return array
     *
     * create by hxc 20171205
     */
    public function search($parameters = null)
    {
        $httpClient = new HttpClient(trim($this->host));

        $postData = [
            //'articleLevel' => 3,
            'content' => isset($parameters['article_keyword']) ? trim($parameters['article_keyword']) : '',
            //'articleType' => 3,
            'page' => isset($parameters['page']) ? (intval($parameters['page'])-1) : 0,
            'pageSize' => isset($parameters['rows']) ? intval($parameters['rows']) : 40,
            'schoolId' => [0],
        ];
        $response = $httpClient->postJson('/api/v1/manage/artilces',$postData);
        $result = self::dealArrForEasyUi(json_decode($response->body));
        return $result;
    }

    /**
     * 文章操作
     *
     * @param null $parameters
     * @return array
     *
     * create by hxc 20171205
     */
    public function articleCtrl($parameters = null)
    {
        $httpClient = new HttpClient(trim($this->host));
        $manageArticleList = array();
        $result = array();

        if(isset($parameters['type']) && strcmp($parameters['type'],'') && self::getVal($parameters['type'],'typeCtrl')){
            $type = self::getVal($parameters['type'],'typeCtrl');
        }
        else{
            return $result;
        }

        if(isset($parameters['articleids']) && !empty($parameters['articleids'])){
            foreach ($parameters['articleids'] as $key => $val) {
                if(strcmp($val,'')){
                    $tempArr = [
                        'articleId' => $val
                    ];
                    array_push($manageArticleList,$tempArr);
                }
            }
        }
        else{
            return $result;
        }

        if(empty($manageArticleList)){
            return $result;
        }

        $postData = [
            'actionType' => $type,
            'manageArticleList' => $manageArticleList
        ];
        $response = $httpClient->postJson('/api/v1/manage/artilces/control',$postData);

        $res = array();
        $res['status'] = $response->header->statusCode;
        if($response->isSuccess()){
            $res['msg'] = '成功';
        }
        else{
            $res['msg'] = '无数据更改';
        }
        return $res;
    }

    /**
     * 学海美文评论之列表
     *
     * @param null $parameters
     * @return array
     *
     * create by hxc 20171205
     */
    public function searchComment($parameters = null){
        $httpClient = new HttpClient(trim($this->host));

        $postData = [
            'content' => isset($parameters['comment_keyword']) ? trim($parameters['comment_keyword']) : '',
            'page' => isset($parameters['page']) ? (intval($parameters['page'])-1) : 0,
            'pageSize' => isset($parameters['rows']) ? intval($parameters['rows']) : 40,
            'schoolId' => [0],
            'userId' => 0
        ];
        $response = $httpClient->postJson('/api/v1/manage/comments',$postData);
        $result = self::dealArrForComment(json_decode($response->body));
        return $result;
    }

    /**
     * 评论操作
     *
     * @param null $parameters
     * @return array
     *
     * create by hxc 20171205
     */
    public function commentCtrl($parameters = null){
        $httpClient = new HttpClient(trim($this->host));
        $manageCommentList = array();
        $result = array();

        if(isset($parameters['type']) && strcmp($parameters['type'],'') && self::getVal($parameters['type'],'typeCtrl')){
            $type = self::getVal($parameters['type'],'typeCtrl');
        }
        else{
            return $result;
        }

        if(isset($parameters['commentids']) && !empty($parameters['commentids'])){
            foreach ($parameters['commentids'] as $key => $val) {
                if(strcmp($val,'')){
                    $tempArr = [
                        'commentId' => $val
                    ];
                    array_push($manageCommentList,$tempArr);
                }
            }
        }
        else{
            return $result;
        }

        if(empty($manageCommentList)){
            return $result;
        }

        $postData = [
            'actionType' => $type,
            'manageCommentList' => $manageCommentList
        ];
        $response = $httpClient->postJson('/api/v1/manage/comments/control',$postData);

        $res = array();
        $res['status'] = $response->header->statusCode;
        if($response->isSuccess()){
            $res['msg'] = '成功';
        }
        else{
            $res['msg'] = '无数据更改';
        }
        return $res;
    }


    /**
     * 处理访问接口返回之数据(文章列表的)
     *
     * @param $result
     * @return array
     *
     * create by hxc 20171205
     */
    public static function dealArrForEasyUi($result){
        if(empty($result->data)){
            return array(['total' => 0,'rows' => '']);
        }

        $data = array();
        $data['rows'] = $result->data;
        $data['total'] = $result->totalCount;
        foreach ($data['rows'] as $key => $value) {
            $data['rows'][$key]->title = mb_substr($value->title,0,30);
            $data['rows'][$key]->content = $value->content;
            $data['rows'][$key]->createTime = date('Y-m-d H:i:s', $value->createTime/1000);
            $data['rows'][$key]->publishedTime = $value->publishedTime ? date('Y-m-d H:i:s', $value->publishedTime/1000) : '';
            $data['rows'][$key]->recallStatus = self::getVal($value->recallStatus,'recallStatus');
            $data['rows'][$key]->payIntegralStatus = self::getVal($value->payIntegralStatus,'payIntegralStatus');
            $data['rows'][$key]->articleLevel = self::getVal($value->articleLevel,'articleLevel');
            $data['rows'][$key]->praiseStatus = self::getVal($value->praiseStatus,'praiseStatus');
            $data['rows'][$key]->articleType = self::getVal($value->articleType,'articleType');
            $data['rows'][$key]->status = self::getVal($value->status,'status');
            $data['rows'][$key]->releaseStatus = self::getVal($value->releaseStatus,'releaseStatus');
            $data['rows'][$key]->collectStatus = self::getVal($value->collectStatus,'collectStatus');
            $data['rows'][$key]->userType = self::getVal($value->userType,'userType');
        }
        return $data;
    }

    /**
     * 处理接口获取文章评论数据
     *
     * @param $result
     * @return array
     *
     * create by hxc 20171205
     */
    public static function dealArrForComment($result){
        if(empty($result->data)){
            return array(['total' => 0,'rows' => '']);
        }
        $data = array();
        $data['rows'] = $result->data ?: [];
        $data['total'] = $result->totalCount;

        foreach ($data['rows'] as $key => $value) {
            $data['rows'][$key]->title = mb_substr($value->title,0,30);
            $data['rows'][$key]->content = $value->content;
            $data['rows'][$key]->status = self::getVal($value->status,'status');
            $data['rows'][$key]->collectStatus = self::getVal($value->collectStatus,'collectStatus');
            $data['rows'][$key]->createTime = date('Y-m-d H:i:s', $value->createTime/1000);
            $data['rows'][$key]->userType = self::getVal($value->userType,'userType');
        }
        return $data;
    }


    /**
     * 返回ID对应的值
     *
     * @param $key
     * @param $name
     * @return string
     *
     * create by hxc 20171205
     */
    public static function getVal($key,$name){
        if(array_key_exists($key,self::${$name})){
            return self::${$name}[$key];
        }
        else{
            return '';
        }
    }

}
